require("../../assets/css/basic.less");
require("../../assets/css/normalize.css");
require("../../assets/css/property.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/font-icon/iconfont.css");
const utils = require("../../libs/utils");
const axios = require("axios");
require("weui");
require("../index/index.less");
const weui = require("weui.js");
const dom = require("../../libs/dom.js");
/**
 * 引入轮播插件
 */
import Swiper, { Navigation, Pagination, Autoplay } from "swiper";
import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";

//文档加载完成事件
document.addEventListener("DOMContentLoaded", function () {
	// 配置轮播图
	Swiper.use([Autoplay, Navigation, Pagination]);

	// 初始化顶部轮播图插件
	const swiper = new Swiper(".slideshow", {
		autoplay: { disableOnInteraction: false },
		loop: true,
		pagination: { el: ".swiper-pagination" },
	});

	// 添加页脚
	utils.footerPlugin(0);

	//发请求获取用户个人信息
	axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";
	let token = localStorage.getItem("token");
	axios.defaults.headers.common["Authorization"] = "Bearer " + token;
	axios.get("/api/user/info").then((res) => {
		let data = res.data.data;
		//渲染排名
		dom.get(".rankNum").textContent = data.ranking;
		//渲染徽章
		dom.get(".badgeNum").textContent = data.badges;
	});

	//发请求获取打卡信息
	function renderClock() {
		axios.get("/api/user/clockInInfo").then((res) => {
			//渲染打卡天数
			dom.get("#punchIn").textContent = res.data.data.clockCount;

			let btn = dom.get(".clockInBtn");
			if (res.data.data.isClockIn) {
				//已打卡
				btn.disabled = true; //禁用
				btn.style.backgroundColor = "#999";
				btn.textContent = "已打卡";
			}
		});
	}
	renderClock();

	//绑定打卡的事件
	dom.get(".clockInBtn").addEventListener("click", function () {
		//发请求打卡
		axios.post("/api/user/clockIn").then((res) => {
			if (res.data.errno === 0) {
				alert("打卡成功，明天再来");
				renderClock();
			}
		});
	});
});
